package com.zd.commodity.supplierMng.dao.impl;

import java.sql.Timestamp;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.zd.commodity.brandMng.entity.BrandShow;
import com.zd.commodity.dao.impl.BaseDao;
import com.zd.commodity.productMng.entity.ProductAttr;
import com.zd.commodity.supplierMng.dao.ISupplierDao;
import com.zd.commodity.supplierMng.entity.Supplier;
import com.zd.commodity.userMng.entity.User;
@Repository("supplierDao")
public class SupplierDaoImpl extends BaseDao<Supplier> implements ISupplierDao {

	@Override
	public DetachedCriteria getDetachedCriteria(Supplier t1, Supplier t2,
			Object param) {
		DetachedCriteria dc=DetachedCriteria.forClass(Supplier.class);
		if(t1!=null){
			if(!"".equals(t1.getName())){
				dc.add(Restrictions.like("name", t1.getName(), MatchMode.ANYWHERE));
			}
		}
		dc.addOrder(Order.desc("creationtime"));
		return dc;
	}

	@Resource(name = "sessionFactory")
	public void setSuperSessionFactory(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	public void add(Supplier t) {
		User user=(User)ServletActionContext.getRequest().getSession().getAttribute("user");
		Timestamp time=new Timestamp(System.currentTimeMillis());
		
		t.setName(t.getName());
		t.setCreator(user.getUsername());
		t.setCreationtime(time);
		
		getHibernateTemplate().save(t);		
	}
	public void update(Supplier t) {

		User user=(User)ServletActionContext.getRequest().getSession().getAttribute("user");
		Timestamp time=new Timestamp(System.currentTimeMillis());
		
		t.setName(t.getName());
		t.setCreator(t.getCreator());
		t.setCreationtime(t.getCreationtime());
		t.setUpdatedtime(time);
		t.setUpdater(user.getUsername());
		getHibernateTemplate().update(t);
	}
}
